/**
 * Copyright &copy; 2012-2015 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.soccer.cms.modules.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.soccer.cms.common.persistence.Page;
import com.soccer.cms.common.service.CrudService;
import com.soccer.cms.modules.cms.dao.RecommenDao;
import com.soccer.cms.modules.cms.entity.Imageinfo;
import com.soccer.cms.modules.cms.entity.Recommen;

/**
 * 推荐功能Service
 * @author zouqh
 * @version 2015-05-28
 */
@Service
@Transactional(readOnly = true)
public class RecommenService extends CrudService<RecommenDao, Recommen> {
	
	@Autowired
	private ImageinfoService imageinfoService;
	
	public Recommen get(String id) {
		
		Recommen recommen = super.get(id);
		Imageinfo imageinfo = new Imageinfo();
		imageinfo.setEntityid(id);
		List<Imageinfo> imageinfoList = imageinfoService.findList(imageinfo);
		recommen.setImageinfoList(imageinfoList);
		return recommen;
	}
	
	public List<Recommen> findList(Recommen recommen) {
		if(recommen.getGroupTypeList()!=null && recommen.getGroupTypeList().size() == 0){
			recommen.setGroupTypeList(null);
		}
		return super.findList(recommen);
	}
	
	public Page<Recommen> findPage(Page<Recommen> page, Recommen recommen) {
		if(recommen.getGroupTypeList()!=null && recommen.getGroupTypeList().size() == 0){
			recommen.setGroupTypeList(null);
		}
		return super.findPage(page, recommen);
	}
	
	@Transactional(readOnly = false)
	public void save(Recommen recommen) {
		List<Imageinfo> imageinfoList = recommen.getImageinfoList();
		super.save(recommen);
		
		//save image
		for (Imageinfo imageinfo : imageinfoList) {
			imageinfo.setEntityid(recommen.getId());
			imageinfoService.save(imageinfo);
		}
		
		//delete children by code
		/*if(!StringUtils.isEmpty(recommen.getId())){
			String parentCode = recommen.getCode()+'-';
			Recommen recp = new Recommen();
			recp.setCode(parentCode);
			dao.deleteByCode(recp);
		}*/
		
		//save children
		List<Recommen> recommenList = recommen.getRecommenList();
		if(recommenList!=null&&recommenList.size()>0){
			for (Recommen rec : recommenList) {
				if("1".equals(rec.getDelFlag())){
					dao.delete(rec);
				}else{
					save(rec);
				}
			}
		}
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Recommen recommen) {
		recommen =  get(recommen.getId());
		List<Imageinfo> imageinfoList = recommen.getImageinfoList();
		imageinfoService.delete(imageinfoList);
		List<Recommen> recommenList = recommen.getRecommenList();
		if(recommenList!=null&&recommenList.size()>0){
			for (Recommen rec : recommenList) {
				delete(rec);
			}
		}
		super.delete(recommen);
	}
	
	@Transactional(readOnly = false)
	public void delete(String ids) {
		if(!StringUtils.isEmpty(ids)){
			String [] idArray = ids.split(",");
			for (String id : idArray) {
				Recommen recommen = get(id);
				delete(recommen);
			}
		}
	} 
	
}